package com.xm.xmapp;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.adapter.InvoiceListAdapter;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.XListView;
import com.xm.xmapp.customview.XListView.IXListViewListener;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.Get_user_invoice_record_listRequsest;
import com.xm.xmapp.response.bean.Get_user_invoice_record_listResponse;

public class InvoiceListActivity extends BaseActivity implements OnClickListener {

	private InvoiceListAdapter adapter;
	private Get_user_invoice_record_listRequsest requestBean;
	private ArrayList<Get_user_invoice_record_listResponse.T> list;

	private int page;
	private XListView xListView;
	private RelativeLayout rel;
	private TextView title;
	private ImageView back;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_invoicelist);

	}

	@Override
	public void initView() {

		xListView = (XListView) findViewById(R.id.xListView);
		rel = (RelativeLayout) findViewById(R.id.rel);
		title = (TextView) findViewById(R.id.title);
		back = (ImageView) findViewById(R.id.back);
	}

	@Override
	public void initData() {

		title.setText("历史记录");
		page = 1;
		requestBean = new Get_user_invoice_record_listRequsest();

		requestBean.pagesize = 20;
		list = new ArrayList<Get_user_invoice_record_listResponse.T>();
		adapter = new InvoiceListAdapter(list, this, R.layout.item_invoice);
		xListView.setAdapter(adapter);
		xListView.setPullRefreshEnable(true);
		xListView.setPullLoadEnable(true);
		get_user_invoice_record_list();

	}

	@Override
	public void initEvent() {
		back.setOnClickListener(this);
		xListView.setXListViewListener(new IXListViewListener() {

			@Override
			public void onRefresh() {
				page = 1;
				xListView.setPullLoadEnable(true);
				get_user_invoice_record_list();
			}

			@Override
			public void onLoadMore() {
				page++;
				get_user_invoice_record_list();
			}
		});

	}

	/**
	 * 获取发票列表
	 */

	private void get_user_invoice_record_list() {

		requestBean.page = page;
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();
				xListView.stopRefresh();
				xListView.stopLoadMore();
				Get_user_invoice_record_listResponse base = JSON.parseObject(response,
						Get_user_invoice_record_listResponse.class);

				if (page == 1)
					list.clear();

				if (base.pageTotal >= page)
					list.addAll(base.list);

				if (base.list == null || base.list.size() == 0 || base.pageTotal <= page) {
					xListView.setPullLoadEnable(false);
				}
				if (list.size() > 0) {
					xListView.setVisibility(View.VISIBLE);
					rel.setVisibility(View.GONE);
				}
				adapter.notifyDataSetChanged();

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
				xListView.stopRefresh();
				xListView.stopLoadMore();
			}
		});

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;

		default:
			break;
		}

	}
}
